//
//  YTSelecteCarViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/8/8.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTSelecteCarViewModel.h"
#import "YTDriverCarServices.h"
#import "YTDriverCarModel.h"

@interface YTSelecteCarViewModel ()
/** 升降序*/
@property (assign, nonatomic) BOOL isDesc;
/** 排序字段：carClass 车辆等级 ，maxPassenger 载客规格*/
@property (copy, nonatomic) NSString *orderColumn;
@property (strong, nonatomic, readwrite) RACCommand *changTypeCmd;

@end

@implementation YTSelecteCarViewModel

- (void)initialize {
    self.page = 1;
    self.orderColumn = @"carClass";
    self.isDesc = 0;
    self.dataArray = [NSMutableArray array];
    @weakify(self);
    self.requestRemoteDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            @strongify(self);
            [self requestDataWith:subscriber];
            return nil;
        }];
    }];
    
    [self loadMoreData];
    [self changeTab];
}

- (void)loadMoreData {
    @weakify(self);
    self.loadMoreDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            @strongify(self);
            [self requestDataWith:subscriber];
            return nil;
        }] initially:^{
            @strongify(self);
            self.page++;
        }];
    }];
}

- (void)changeTab {
    @weakify(self);
    self.changTypeCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(RACTuple *input) {
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            @strongify(self);
            [self requestDataWith:subscriber];
            return nil;
        }] initially:^{
            @strongify(self);
            self.orderColumn = input.first;
            self.isDesc = [input.second integerValue];
            [self.dataArray removeAllObjects];
        }];
    }];
}

- (void)requestDataWith:(id<RACSubscriber>)subscriber {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"page"] = [NSString stringWithFormat:@"%zd", self.page];
    dict[@"isBind"] = @"1";
    dict[@"desc"] = [NSString stringWithFormat:@"%zd", self.isDesc];
    dict[@"orderColumn"] = self.orderColumn;
    [[YTDriverCarServices teamDriverList:dict] subscribeNext:^(NSDictionary *response) {
        NSArray<YTDriverCarModel *> *array = [NSArray yy_modelArrayWithClass:[YTDriverCarModel class] json:response[@"responseData"]];
        [self.dataArray addObjectsFromArray:array];
        [subscriber sendNext:array];
        [subscriber sendCompleted];
    }];
}

@end
